const app = getApp()
var that;
var util = require('../../utils/util.js');
Page({

  /* 页面的初始数据 */
  data: {
    userinfo:''
  },

  /* 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    that=this
    that.setData({
      userinfo: app.globalData.loginUserObj
    })
  },

  // 编辑个人信息
  toEditUserFun:function(){
    wx.navigateTo({
      url: '/pages/pageMeEdit/pageMeEdit',
    })
  },
  
  // 展示二维码
  toCodeImgFun:function(){
    wx.navigateTo({
      url: '/pages/pageCodeImg/pageCodeImg',
    })
  },

  //赋值剪切板
  copyJD: function () {
    var thah=this;
    wx.setClipboardData({
      data: thah.data.userinfo.ydCompany.code,
      success: function (res) {
        wx.showModal({
          title: '提示',
          content: '复制成功',
          success: function (res) {
            if (res.confirm) {
              console.log('确定')
            } else if (res.cancel) {
              console.log('取消')
            }
          }
        })
      }
    })
  },

  //退出组织
  SignOutJD(){
    that = this;
    console.log(app.globalData.loginUserObj)
    wx.request({
      url: app.http + 'hysUser/memberList/quit',
      data: {
        companyId: app.globalData.loginUserObj.ydCompany.id,
        userId: app.globalData.loginUserObj.id,
      },
      method: 'post',
      header: {
        'content-type': 'application/json', // 默认值
        'APP-User-Token': app.globalData.tokenMap.access_token
      },
      success: function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '已退出组织',
            icon: 'success',
            duration: 2000,
            success(){
              setTimeout(function () {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }, 1000)
            }
          })
        }
        else if (res.data.code == 100101) {
          // token无效，获取新的token
          var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
            function (res) {
              if (res.data.code == 200) {
                that.SignOutJD();
              }
              else if (res.data.code == 400) {
                var wxLoginFun = util.wxLoginFun().then(
                  function (res2) {
                    if (res2.data.code == 200) {
                      that.SignOutJD();
                    }
                  }
                )
              }
            }
          ).catch(
            function (err) {
              console.log("获取新的token失败返回", err)
            }
          )
        }
      },
      fail: function (err) {
        console.log("fail返回", err)
      }
    })
  }

})